package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.exception.ServiceException;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AlarmRuleMapper;
import com.ruoyi.system.domain.AlarmRule;
import com.ruoyi.system.service.IAlarmRuleService;

/**
 * 指令配置Service业务层处理
 * 
 * @author wp
 * @date 2023-08-10
 */
@Service
public class AlarmRuleServiceImpl extends ServiceImpl<AlarmRuleMapper, AlarmRule> implements IAlarmRuleService {

    @Autowired
    private AlarmRuleMapper alarmRuleMapper;

    @DataScope
    @Override
    public List<AlarmRule> selectList(AlarmRule alarmRule) {
        return alarmRuleMapper.list(alarmRule);
    }

    @Override
    public int removeByIds(List<Long> ids) {
        List<Long> bindFenceRuleIds = alarmRuleMapper.getBindFenceRuleIds(ids);
        List<Long> bindVehicleRuleIds = alarmRuleMapper.getBindVehicleRuleIds(ids);
        // 如果绑定了围栏或监管对象，不予删除，返回提示
        if (CollectionUtils.isNotEmpty(bindFenceRuleIds) || CollectionUtils.isNotEmpty(bindVehicleRuleIds)) {
            String exception = "";
            if (CollectionUtils.isNotEmpty(bindFenceRuleIds)) {
                List<String> bindFenceRules = alarmRuleMapper.selectBatchIds(bindFenceRuleIds).stream().map(AlarmRule::getName).collect(Collectors.toList());
                exception += "<p>以下规则已绑定围栏，请先解绑后删除：" + bindFenceRules.toString() + "</p>";
            }
            if (CollectionUtils.isNotEmpty(bindVehicleRuleIds)) {
                List<String> bindVehicleRules = alarmRuleMapper.selectBatchIds(bindVehicleRuleIds).stream().map(AlarmRule::getName).collect(Collectors.toList());
                exception += "<p>以下规则已绑定监管对象，请先解绑后删除：" + bindVehicleRules.toString() + "</p>";
            }
            throw new ServiceException(exception);
        }
        return alarmRuleMapper.deleteBatchIds(ids);

    }
}
